package com.ocean.security.filter;

import com.ocean.cache.redis.IRedisCache;
import com.ocean.core.util.JwtUtil;
import com.ocean.core.util.constants.ConstantsCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * token过滤器
 *
 * @author zhang
 * @since 2021/9/13
 */
@Component
public class CustomOncePerRequestFilter extends OncePerRequestFilter {

    @Autowired
    private IRedisCache iRedisCache;

//    @Override
//    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
//                                    FilterChain chain) throws ServletException, IOException {
//        String backToken = request.getHeader(ConstantsMethods.BACKSTAGE);
//        String deskToken = request.getHeader(ConstantsMethods.DESKTOP);
//        //判断token情况，给予对应的处理方案
//        if (JwtUtil.checkToken(backToken)) {
//            String userId = JwtUtil.parseJwt(backToken).getId();
//            SysUserTo userTo = (SysUserTo) iRedisCache.get(ConstantsCache.ADMIN_PREFIX + userId);
//            this.authentication(userTo);
//        } else if (JwtUtil.checkToken(deskToken)) {
//            String memberId = JwtUtil.parseJwt(deskToken).getId();
//            OceanMemberTo memberTo = (OceanMemberTo) iRedisCache.get(ConstantsCache.CONSUMER_PREFIX + memberId);
//            this.authentication(memberTo);
//        }
//        chain.doFilter(request, response);
//    }


    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        String authentication = request.getHeader(HttpHeaders.AUTHORIZATION);
        if (JwtUtil.checkToken(authentication)) {
            String userId = JwtUtil.parseJwt(authentication).getId();
            UserDetails userDetails = (UserDetails) iRedisCache.get(ConstantsCache.getInstance().getAuthenticationPrefix() + userId);
            this.authentication(userDetails);
        }
        filterChain.doFilter(request, response);
    }

    public void authentication(UserDetails userDetails) {
        if (userDetails != null) {
            UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails,
                    null, userDetails.getAuthorities());
            authentication.setDetails(authentication);
            SecurityContextHolder.getContext().setAuthentication(authentication);
        }
    }
}
